﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using SBPweb.Modules.Smartparts;

using SBPweb.MediaCenter.Common.Services;
using SBPweb.MediaCenter.Tools.WPF.Controls;

namespace SBPweb.MediaCenter.Presentation.WPF.Controls
{

    public class SmartpartListView : LayoutManager<SmartpartList>, ILayoutService 
    {

        #region Constructor logic

        public SmartpartListView(Window window)
            : this(window, null)
        {
        }

        public SmartpartListView(Window window, Panel visual)
            : base(window, visual, Activator.CreateInstance<SmartpartList>())
        {
            List.ItemAdded += new EventHandler<SmartpartEventArgs>(List_ItemAdded);
            List.ItemRemoved += new EventHandler<SmartpartEventArgs>(List_ItemRemoved);
        }

        #endregion

        #region Property accessors

        #endregion

        #region Event handlers

        private void List_ItemAdded(object sender, SmartpartEventArgs e)
        {
            if (e.Smartpart.ToString().StartsWith("SBPweb.MediaCenter.Modules.Organizer.WPF.Controls"))
            {
                base.Add(ConvertSmartpartToUIElement(e.Smartpart));
            }
        }

        private void List_ItemRemoved(object sender, SmartpartEventArgs e)
        {
//            base.Remove(ConvertSmartpartToUIElement(e.Smartpart));
        }

        #endregion

        #region ILayoutService Members

        public void BringToFront(ISmartpart smartpart)
        {
            base.BringToFront(ConvertSmartpartToUIElement(smartpart));
        }

        public void BringToFront(List<ISmartpart> smartparts)
        {
            base.BringToFront(smartparts.ConvertAll<UIElement>(ConvertSmartpartToUIElement));
        }

        public void SendToBack(ISmartpart smartpart)
        {
            base.SendToBack(ConvertSmartpartToUIElement(smartpart));
        }

        public void SendToBack(List<ISmartpart> smartparts)
        {
            base.SendToBack(smartparts.ConvertAll<UIElement>(ConvertSmartpartToUIElement));
        }

        #endregion

        public static UIElement ConvertSmartpartToUIElement(ISmartpart smartpart)
        {
            return smartpart as UIElement;
        }

    }

}